package Duoxiancheng;
//lock锁
import java.util.concurrent.locks.ReentrantLock;

public class Locks {
    public static void main(String[] args) {
        TestLosk testLosk=new TestLosk();
        new Thread(testLosk,"售票员1").start();
        new Thread(testLosk,"售票员2").start();
        new Thread(testLosk,"售票员3").start();
    }
}

class TestLosk implements Runnable {
    int ticket = 10;
    private final ReentrantLock lock = new ReentrantLock();

    @Override
    public void run() {
        lock.lock();
            try {
                while (true) {
                    if (ticket > 0) {
                        try {
                            Thread.sleep(1000);
                            System.out.println(ticket--);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    } else {
                        break;
                    }
                }
            } finally {
                lock.unlock();
            }
    }
}